package com.example;

public class ThreadWaitingWithJoin {

    public static class TestThread extends Thread{
        public TestThread() {
            super("test-thread");
        }

        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000*10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {

        TestThread testThread = new TestThread();
        testThread.start();

        Thread mainThread = Thread.currentThread();
        new Thread(()->{
            try {
                Thread.sleep(1000*3);

                System.out.println("主线程的状态：" + mainThread.getState());
                System.out.println("开始中断主线程...");
                mainThread.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread3").start();

        try {
            // 等待test-thread运行结束
            testThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
